<?php
/**
 * Documentation, License etc.
 *
 * @package girafplace
 */
require_once 'include.php';
require_once '_functions.php';

?>
<html>
<head>
<link type="text/css" href="css/ui-lightness/jquery-ui-1.8.11.custom.css" rel="stylesheet" />
<link type="text/css" href="css/giraf.css" rel="stylesheet" />

<script type="text/javascript" src="js/jquery-1.5.1.min.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.8.11.custom.min.js"></script>
<script type="text/javascript" > $(document).ready(function(){ 
  $("#applicationAccordion").accordion({ header: "h3" });
  $("ul#icons li").hover(
					function() { $(this).addClass("ui-state-hover"); }, 
					function() { $(this).removeClass("ui-state-hover"); }
				);
  $("#apkHelp").dialog({
			autoOpen: false,
			modal: true,
			buttons: {
				Ok: function() {
					$( this ).dialog( "close" );
				}
			}
		});
  $( "#apkHelpIcon" )
			.click(function() {
				$( "#apkHelp" ).dialog( "open" );
			});
$("#addApplicationButton").button()
			  .click(function() {
				$( "#addApplicationDialog" ).dialog( "open" );
			});
$("#logoutButton").button();
$("#iconHelp").dialog({
			autoOpen: false,
			modal: true,
			buttons: {
				Ok: function() {
					$( this ).dialog( "close" );
				}
			}
		});
$("#addApplicationDialog").dialog({
			autoOpen: false,
			modal: true,
			height: 460,
			width: 850,
			buttons: {
				Ok: function() {
					$("#addApplicationForm").submit();
				},
				Cancel: function() {
					$( this ).dialog( "close" );
				}
			
			}
		});

  $( "#iconHelpIcon" )
			.click(function() {
				$( "#iconHelp" ).dialog( "open" );
			});
$( "#editAdminButton").button()
	.click(function() {
		$( "#editAdminDialog" ).dialog( "open" );
});

$("#registerButton").button()
        .click(function() {
            $("#registerDialog").dialog("open");
});

$("#loginButton").button()
        .click(function() {
            $("#loginForm").submit();
});

$( "#editAdminDialog").dialog({
	autoOpen: false,
	modal: true,
	height: 460,
	width: 650,
	buttons: {
		Ok: function() {
			$ ("#editAdminForm").submit();
		},
			Cancel: function() {
			$ (this).dialog("close");
		}
	}
});
$( "#registerDialog").dialog({
        autoOpen: false,
        modal: true,
        height: 300,
        width: 400,
        buttons: {
                Ok: function() {
                        $ ("#register").submit();
                },
                        Cancel: function() {
                        $ (this).dialog("close");
                }
        }

});
});
 </script>

<!-- The following css is from the jquery demo page, and is here to make everything look pretty  -->
<style type="text/css">
			/*demo page css*/
			body{ font: 10pt "Trebuchet MS", sans-serif; margin: 50px;}
			.demoHeaders { margin-top: 2em; }
			#dialog_link {padding: .4em 1em .4em 20px;text-decoration: none;position: relative;}
			#dialog_link span.ui-icon {margin: 0 5px 0 0;position: absolute;left: .2em;top: 50%;margin-top: -8px;}
			ul#icons {margin: 0; padding: 0;}
			ul#icons li {margin: 2px; position: relative; padding: 4px 0; cursor: pointer; float: left;  list-style: none;}
			ul#icons span.ui-icon {float: left; margin: 0 4px;}
		</style>

</head>
<body>
<?
if(!$loggedin) {
?>
<a id="registerButton" href="#">Register New User</a>

<div class="ui-widget ui-widget-content ui-corner-all login-box">
<div class="ui-widget-header login-box-header">Login</div>
<form id="loginForm" name="login" action="login.php" method="POST" >
<table>  
<tr><td>Email:</td> <td><input type="text" name="username" /></td></tr>
<tr><td>Password:</td><td><input type="password" name="password" /></td></tr>
<tr><td colspan="2" align="right"><a id="loginButton" href="#">Login</a></td></tr>
</table>
</form>
</div>

<div id="registerDialog" title="Register a new user">
<form id="register" name='register' action='register.php' method='POST'>
<table>
<tr><td colspan='2'><center>REGISTER NEW USER</center></td></tr>
<tr border='none'><td>Email (also your username):</td><td><input type='text' name='email'/></td></tr>
<tr><td>Name:</td><td><input type='text' name='realname'/></td></tr>
<tr><td>Password:</td><td><input type='password' name='password'/></td></tr>
<tr><td>Retype Password:</td><td><input type='password' name='password2'/></td></tr></table>
</form>
</div>
<?php
} else {

  echo '<a id="addApplicationButton" href="#" >Add Application</a>';
  echo '<a id="editAdminButton" href="#">Edit admin</a>';
  echo '<a id="logoutButton" href="logout.php">Logout</a>';

	Connect();
	
	//Getting the abilities
	$abilQuery = "SELECT * FROM abilities";
	
	$abilResult = mysql_query($abilQuery);
	

	if(!$abilResult)
		throw new Exception("There was a problem getting the abilities");
	
	$abils = array();

	while($abilRow = mysql_fetch_row($abilResult)) {
		$abils[] = $abilRow;
	}

	//Retrieve admin info (used to admin editing)
	$rs = getAdmin($_SESSION['adminId']);
	$admin = mysql_fetch_assoc($rs);
	
  //Retrieve all applications of an administrator
  $result = getApplications($_SESSION['adminId']);
  
  if(!$result)
    die('There was an error with the database: ' . mysql_error());
  
  echo '<div id="applicationAccordion">';
	while ($row = mysql_fetch_row($result)) { ?>
		<h3><a href="#"><?echo $row[1]?><? if($row[7] == 'ERROR') { 
                    echo " - Errors";
                } ?></a></h3><div>
                <? if($row[7] == 'ERROR') { ?>
		<a id="apperror<?echo $row[0]?>" href="#" >Application errors</a>
                <? } ?>
		<form action="editApplication.php" method="POST">
		<input type="hidden" name="id" value="<?echo $row[0]?>" />
		<table>
		<tr><td>Name:</td> <td><input name="name" type="text" class="text ui-widget-content ui-corner-all" value="<?echo$row[1]?>" /></td> 
		<td rowspan="4"> 
		<?php
			$abilities = mysql_query("SELECT canRead, canDragAndDrop, canHear, requiresSimpleVisualEffects, canAnalogTime, canDigitalTime, hasBadVision, requiresLargeButtons, canSpeak, canNumbers, canUseKeyboard FROM applications WHERE applicationId = " . $row[0]);
			$rs = mysql_fetch_assoc($abilities);
			
			echo '<input type="checkbox" name="abilities[]" value="1"';
			if ($rs['canDragAndDrop'] == 1) { echo " checked=YES"; }
			echo '/>can drag and drop.<br/>';
			
			echo '<input type="checkbox" name="abilities[]" value="2"';
			if ($rs['canHear'] == 1) { echo " checked=YES"; }
			echo '/>Can hear.<br/>';
			
			echo '<input type="checkbox" name="abilities[]" value="3"';
			if ($rs['requiresSimpleVisualEffects'] == '1') { echo " checked=YES"; }
			echo '/>Requires simple visual effects.<br/>';
			
			echo '<input type="checkbox" name="abilities[]" value="4"';
			if ($rs['canAnalogTime'] == '1') { echo " checked=YES"; }
			echo '/>Can analog time.<br/>';
			
			echo '<input type="checkbox" name="abilities[]" value="5"';
			if ($rs['canDigitalTime'] == '1') { echo " checked=YES"; }
			echo '/>Can digital time.<br/>';
			
			echo '<input type="checkbox" name="abilities[]" value="6"';
			if ($rs['canRead'] == '1') { echo " checked=YES"; }
			echo '/>Can read.<br/>';
			
			echo '<input type="checkbox" name="abilities[]" value="7"';
			if ($rs['hasBadVision'] == '1') { echo " checked=YES"; }
			echo '/>Has bad vision.<br/>';
			
			echo '<input type="checkbox" name="abilities[]" value="8"';
			if ($rs['requiresLargeButtons'] == '1') { echo " checked=YES"; }
			echo '/>Requires large buttons.<br/>';
			
			echo '<input type="checkbox" name="abilities[]" value="9"';
			if ($rs['canSpeak'] == '1') { echo " checked=YES"; }
			echo '/>Can speak.<br/>';
			
			echo '<input type="checkbox" name="abilities[]" value="10"';
			if ($rs['canNumbers'] == '1') { echo " checked=YES"; }
			echo '/>Can numbers.<br/>';
			
			echo '<input type="checkbox" name="abilities[]" value="11"';
			if ($rs['canUseKeyboard'] == '1') { echo " checked=YES"; }
			echo '/>Can use keyboard.<br/>';
		?>
		
		</td></tr> 
		<tr><td>Description:</td> <td> <textarea name="description" cols="50" rows="10" class="text ui-widget-content ui-corner-all" ><?echo $row[2]?> </textarea></td></tr>
		<tr><td></td><td><input type="submit" value="Upload Changes" /></td></tr>
		</table></form>
                <? if($row[7] == 'ERROR') { ?>
		<div id="errorDialog<? echo $row[0] ?>">
		<? $_GET['id'] = $row[0]; include('errors.php') ?>
		</div>
		<script type="text/javascript" > 
		$(document).ready(function(){ 
			$( "#errorDialog<? echo $row[0] ?>").dialog({
				autoOpen: false,
				modal: true,
				buttons: {
					Ok: function() {
						$ (this).dialog("close");
					}	
				}
			});

			$( "#apperror<? echo $row[0] ?>").button()
				.click(function() {
					$( "#errorDialog<? echo $row[0] ?>" ).dialog( "open" );
				});
		});
                </script> 
                <? } ?>
      <?

	echo '</div>';

  }
  echo '</div>';
Close();

?>
<div id="addApplicationDialog" title="Add Application">  
  <form id="addApplicationForm" enctype="multipart/form-data" action="addApplication.php" method="POST">
    <table>
      <tr>
	<td><label for="name">Name:</label></td>
	<td><input name="name" type="text" class="text ui-widget-content ui-corner-all"/></td>
	<td rowspan="2">
	<?
		foreach($abils as $abil) {
			?>
			<input type="checkbox" name="abilities[]" value="<?echo $abil[0]?>" /> <? echo $abil[1] ?> <br />
			<? 
		}
	?></td>
      </tr>
      <tr>
	<td><label for=description>Description:</label> </td>
	<td><textarea name="description" cols="50" rows="10" class="text ui-widget-content ui-corner-all"></textarea> </td>
      </tr>
      <tr>
	<td><label for="apk">APK File:</label> </td>
	<td><input name="apk" type="file" class="text ui-widget-content ui-corner-all"/> </td>
      </tr>
    </table>
  </form>
</div>

  <div id="apkHelp" title="APK file?">
    <p>The APK file should only be changed if you wish to upload a new APK file. If left empty it will not remove the current file.</p>
  </div>

  <div id="iconHelp" title="Icon file?">
    <p>The icon file should only be changed if you wish to upload a new icon file. If left empty it will not remove the current file.</p>
  </div>

  
	<div id='editAdminDialog' title='Edit administrator information'>
	<form id='editAdminForm' action='editadmin.php' method='post'>
		<table>
		<tr><td colspan='3'><center>Edit administrator information</center></td></tr>
		<tr><td>Administrator Name</td><td><input type='text' name='realname' value='<?php echo $admin['adminName']?>'></td></tr>
		<tr><td>Administrator Email</td><td><input type='text' name='email' value='<?php echo $admin['email']?>'></td></tr>
		<tr><td>New password</td><td><input type='password' name='newpassword1'></td></tr>
		<tr><td>Retype new password</td><td><input type='password' name='newpassword2'></td></tr>
		<tr><td>Old password</td><td><input type='password' name='oldpassword'></td></tr>
		<input type='hidden' name='oldmail' value='<?php echo $admin['email']?>'></td></tr>
		</table>
		</form>
		<p>Old password must be entered to update - new password (2 fields) should only be entered if password of the account should be changed.</p>
	</div>
	
<?php
}

echo '</body>';

?>
